import CryptoJS from 'crypto-js';
import { NextResponse } from 'next/server'
import { httpCode } from './httpCode.mjs'
export { httpCode }




const key = CryptoJS.enc.Utf8.parse('your_secret_key');
const iv = CryptoJS.enc.Utf8.parse('your_initialization_vector');

function encrypt(text) {
    const ciphertext = CryptoJS.AES.encrypt(text, key, { iv: iv }).toString();
    return ciphertext;
}
/**
 * Returns a Response object with a JSON body
 */
export function jsonResponse(status, data = {}, init) {
    const sendData = process.env.ENCHPHER == 1 ? encrypt(JSON.stringify(data)) : data;
    return new NextResponse(JSON.stringify({ code: status, data: sendData }), {
        ...init,
        status: 200,
        headers: {
            ...init?.headers,
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
            'Access-Control-Allow-Headers': 'Content-Type, Authorization',
        },
    })
}
/**
 * Returns a Response object with a JSON body
 */
export function returnResponse(data, init) {
    return jsonResponse(httpCode.OK, data, init)
}

export function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms))
}
